package main

import (

	"strconv"
	"github.com/boltdb/bolt"
	"fmt"
	"github.com/xuri/excelize"
	"match-spider/util/boltdb"
	"strings"
	"match-spider/util/format2"
)


func checkAsia5(data string) (bool,float64,float64,float64,float64,[]float64) {

	asiaSlice:=strings.Split(data,"!")

	//开盘公司至少10家
	size:=len(asiaSlice)
	if size<10{
		return false,float64(0),float64(0),float64(0),float64(0),nil
	}
	//初盘盘口-0.25~0.25
	i:=int(0)
	j:=int(0)
	m:=int(0)
	len:=int(0)
	//初盘均值
	s1:=float64(0)
	//终盘均值
	s2:=float64(0)
	oddsData:=make(map[string][]string)
	asia0:=make(map[string]int64)
	asia:=make(map[string]int64)
	for _,v:=range asiaSlice{
		asiaOdds:=strings.Split(v,"^")
		name:=asiaOdds[0]
		odds,_:=strconv.ParseFloat(asiaOdds[3],64)
		odds2,_:=strconv.ParseFloat(asiaOdds[6],64)
		s1=s1+odds
		s2=s2+odds2
		if !(odds>=-0.25&&odds<=0.25){
			i++
		}
		if odds==odds2&&odds2==0{
			//初盘，终盘不能全都是0盘口不变化
			j++
		}
		if odds2>0.5||odds2<(-0.5){
			//终盘不在范围的去掉
			m++
		}
		//以终盘中大众盘口作为最终终盘口
		if asia[asiaOdds[6]]==0{
			asia[asiaOdds[6]]=1
		}else{
			asia[asiaOdds[6]]=asia[asiaOdds[6]]+1
		}
		if asia0[asiaOdds[3]]==0{
			asia0[asiaOdds[3]]=1
		}else{
			asia0[asiaOdds[3]]=asia0[asiaOdds[3]]+1
		}
		oddsData[name]=asiaOdds
		len++
	}
	//最多3家公司盘口不为0.25
	if i>3{
		return false,float64(0),float64(0),float64(0),float64(0),nil
	}
	/*if len-j<3{
		//在0盘口至少有3家公司变盘
		return false,float64(0),float64(0),float64(0),float64(0),nil
	}*/
	if m>3{
		return false,float64(0),float64(0),float64(0),float64(0),nil
	}

	//赔率不能太低，也不能太高
	/*ff:=format2.AsiaOdds(data)
	if ff[2]-ff[3]>0.12||ff[4]-ff[5]>0.12{
		return false,float64(0)
	}
	*/
	f1:=oddsData["ＳＢ"]
	f2:=oddsData["Bet365"]
	f3:=oddsData["韦德"]
	f4:=oddsData["易胜博"]
	f5:=oddsData["明陞"]
	f6:=oddsData["10BET"]
	f7:=oddsData["金宝博"]
	f8:=oddsData["12bet/大发"]
	f9:=oddsData["盈禾"]
	f10:=oddsData["18Bet"]
	f11:=oddsData["澳门"]
	d:=[][]string{f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11}
	slice:=make([]float64,0)
	for _,v:=range d{
		if v!=nil{
			ff1,_:=strconv.ParseFloat(v[2],64)
			ff2,_:=strconv.ParseFloat(v[3],64)
			ff3,_:=strconv.ParseFloat(v[4],64)
			ff4,_:=strconv.ParseFloat(v[5],64)
			ff5,_:=strconv.ParseFloat(v[6],64)
			ff6,_:=strconv.ParseFloat(v[7],64)
			slice=append(slice,ff1,ff2,ff3,ff4,ff5,ff6)
		}else{
			slice=append(slice,float64(-9999),float64(-9999),float64(-9999),float64(-9999),float64(-9999),float64(-9999))
		}

	}
	//初盘
	n0:=float64(-9999)
	temp0:=int64(0)
	//终盘
	n:=float64(-9999)
	temp:=int64(0)
	for k,v:=range asia0{
		if v>temp0{
			temp0=v
			n0,_=strconv.ParseFloat(k,64)

		}
	}
	for k,v:=range asia{
		if v>temp{
			temp=v
			n,_=strconv.ParseFloat(k,64)

		}
	}
	return true,n0,n,s1/float64(len),s2/float64(len),slice
}
func checkOverunder5(data string) (bool,float64) {
	overunderSlice:=strings.Split(data,"!")

	total:=float64(0)
	for _,v:=range overunderSlice{
		d:=strings.Split(v,"^")
	 	if len(d)>1{
			val,_:=strconv.ParseFloat(d[6],64)
			total+=val
		}
	}
	avg:=total/float64(len(overunderSlice))

	if avg>=2&&avg<3{
		return true,avg
	}
	return false,float64(0)
}
func score5(fn func([]interface{},int)){
	i:=0
	boltdb.ScanTx("matchs-2017", func(tx *bolt.Tx) {

		tx.ForEach(func(name []byte, b *bolt.Bucket) error {
			b.ForEach(func(k, v []byte) error {
				content:=string(v)
				s:=strings.Split(content,"^")

				league:=s[1]
				startAt:=s[2]
				t1:=s[3]
				t2:=s[5]
				s1,_:=strconv.ParseFloat(s[4],64)
				s2,_:=strconv.ParseFloat(s[6],64)


				boltdb.Get("asia-2017",league,string(k), func(asia string) {
					b1,kk0,kk,avg1,avg2,f1:=checkAsia5(asia)
					if !b1{
						return
					}

					boltdb.Get("lastmatch-2017",league,string(k), func(text string) {
						//判断输赢
						//去掉没有联赛的比赛
						if !strings.Contains(text,"总")||!strings.Contains(text,"BaseInfo"){
							return
						}

						b,ff:=format2.FormatData(league,string(k),startAt,t1,t2,text,asia)
						if b{
							i++
							w:=0

							if s1-kk-s2<0{
								w=1
							}
							slice:=[]interface{}{string(k),w,kk0,kk,avg1,avg2}
							for _,v:=range ff{
								for _,vv:=range v {
									slice=append(slice,vv)
								}

							}
							for _,v:=range f1{
								slice=append(slice,v)
							}
							fmt.Println(i)
							fn(slice,i)
						}
					})

				})

				return nil
			})
			return nil
		})

	})
	fmt.Println(i)
}

func main()  {
	e:=[]string{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}
	xlsx := excelize.NewFile()

	categories := []string{

	}
	for k,v:=range categories{
		len:=int(k/26)
		s:=""
		if len>0{
			s+=e[len-1]+e[k%26]
		}else{
			s+=e[k%26]
		}

		xlsx.SetCellValue("Sheet1",s+"1", v)
	}
	score5(func(s []interface{}, i int) {
		for k,v:=range s{
			len:=int(k/26)
			s:=""
			if len>0{
				s+=e[len-1]+e[k%26]
			}else{
				s+=e[k%26]
			}
			xlsx.SetCellValue("Sheet1",s+strconv.Itoa(i+1), v)
		}
	})
	err := xlsx.SaveAs("./Workbook.xlsx")
	if err != nil {
		fmt.Println(err)
	}

}
